package com.pig.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

      HttpSession session=request.getSession();
      if (session.getAttribute("accountName") == null) {
            session.setAttribute("msg", "你还没有登录哦！");
            System.out.println("尚未登录，调到登录页面");
            response.sendRedirect(request.getContextPath()+"/");
            return false;
        }
        return true;
    }

}
